package com.byedu.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;

/**
 * Created by jason on 2017/6/4.
 */
public class ExcelUtil {
    protected final static Logger logger = LoggerFactory.getLogger(ExcelUtil.class.getName());

    public static XSSFWorkbook createXSSFWorkbook(File excelFile) {
        XSSFWorkbook xssfWorkbook = null;
        InputStream is = null;
        try {
            is = new FileInputStream(excelFile);
            xssfWorkbook = new XSSFWorkbook(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                logger.error("流文件关闭异常 ，{}", e);
            }

        }
        return xssfWorkbook;
    }
    public static HSSFWorkbook createHSSFWorkbook(File excelFile) {

        HSSFWorkbook hssfWorkbook = null;
        InputStream is = null;
        try {
            is = new FileInputStream(excelFile);
            hssfWorkbook = new HSSFWorkbook(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                logger.error("流文件关闭异常 ，{}", e);
            }

        }
        return hssfWorkbook;
    }


//    public static Workbook createHSSFWorkbook(InputStream in) {
//        Workbook rwb = null;
//        try {
//            rwb = new XSSFWorkbook(in);
//
//        } catch (Exception e) {
//            try {
//                rwb = new HSSFWorkbook(in);
//            } catch (Exception ex1) {
//                logger.error("转换excel文件格式不正确", ex1);
//            }
//        } finally {
//            try {
//                if (in != null) {
//                    in.close();
//                }
//            } catch (IOException e) {
//                logger.error("流文件关闭异常 ，{}", e);
//            }
//        }
//        return rwb;
//    }

    public static String getCellValue(Cell cell) {
        String cellValue = "";
        if (cell == null) {
            return cellValue;
        } else {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    NumberFormat formater = NumberFormat.getInstance();
                    formater.setGroupingUsed(false);
                    cellValue = formater.format(cell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_STRING:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BLANK:
                    cellValue = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
                    break;
                case Cell.CELL_TYPE_ERROR:
                    cellValue = String.valueOf(cell.getErrorCellValue());
                    break;
                default:
                    cellValue = cell.getStringCellValue();
            }
        }
        return cellValue.trim();
    }
}
